package com.gg.user.gguser.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gg.cookice.cookice;
import com.gg.encryption.AES;
import com.gg.user.gguser.dao.GgUserDaoMapper;
import com.gg.user.gguser.dataobject.GgUserDataobject;
import com.gg.user.gguser.service.GgLoginService;
import com.gg.util.BaseCommon;
import com.gg.vo.return_format;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author yangxinlei
 */
@Service
public class GgLoginServiceImpl implements GgLoginService {

    @Autowired
    private GgUserDaoMapper ggUserDaoMapper;

    /**
     * 登陆
     * @param response
     * @param request
     * @param name
     * @param password
     * @return
     */
    @Override
    public Object login(HttpServletResponse response, HttpServletRequest request,String name, String password) {
        //查询用户是否存在
        Integer getuser =  ggUserDaoMapper.getUserNameSelectCount(name);
        if (getuser==0){
            //用户不存在
            return return_format.return_format_All(100001,"","用户登录账号或密码错误请重试!");
        }
        //判断用户状态
        GgUserDataobject userState = ggUserDaoMapper.getUserNameSelect(name);
        if(userState.getGgStatus()==1){
            return return_format.return_format_All(100003,"","您的账号已经被禁用请联系管理员解封!");
        }

        try {
            GgUserDataobject ggUserDataobject = ggUserDaoMapper.getUserNameSelect(name);
            String getpasseword = ggUserDataobject.getGgPassword();
            //取出加盐
            String gettoken =  ggUserDataobject.getToken();
            //密码解密
            // AES se=new AES();
            String mpassword = AES.AESDncode(gettoken,getpasseword);

            // 前端密码解密
            password = (String) BaseCommon.decrypt(password);
            if (mpassword.equals(password)){
                Object userInformation = ggUserDaoMapper.getUserNameSelectQ(name);
                //cookice写入数据
                cookice.addCookice(response,"gg", URLEncoder.encode((String) BaseCommon.codebase(JSONObject.toJSONString(userInformation)), "utf-8"));

                // 解密
                //Object ceshi = BaseCommon.decrypt(URLDecoder.decode(URLEncoder.encode(a, "utf-8")));

                // 用户可以看到的用户信息
                return return_format.return_format_All(0,userInformation,"");
            }else {
                return return_format.return_format_All(100001,"","用户登录账号或密码错误请重试!");
            }
        }catch (Exception e){
            return return_format.return_format_All(1003,"","网络异常请重试！");
        }
    }

    /**
     * 注册
     */
    @Override
    public Object register(HttpServletResponse response, HttpServletRequest request, String name, String password) {
        return null;
    }

    /**
     * 退出登陆
     */
    @Override
    public Object signOut(HttpServletResponse response, HttpServletRequest request) {
        try {
            //删除cookice
            cookice.delectCookice(request,response,"gg");
            return return_format.return_format_All(0,"","");
        }catch (Exception e){
            return return_format.return_format_All(1003,"","网络异常请重试！");
        }

    }

}
